package user

import (
	"context"
	"fmt"
	"strings"

	"mall/service/backend/common/utils/pointy"
	client "mall/service/recommended/common/gorseclient"
	"mall/service/recommended/rpc/internal/svc"
	"mall/service/recommended/rpc/types/recommended"

	"github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
	return &CreateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateUserLogic) CreateUser(in *recommended.UserInfo) (*recommended.BaseRowAffectedResp, error) {
	if in.UserId == nil || len(strings.TrimSpace(*in.UserId)) == 0 {

	}
	logrus.Info(fmt.Sprintf("CreateUser UserID: %+v", *in.UserId))
	var user client.User
	user.UserId = *in.UserId
	if len(in.Labels) > 0 {
		user.Labels = in.Labels
	}
	if len(in.Subscribe) > 0 {
		user.Subscribe = in.Subscribe
	}
	if in.Comment != nil && len(strings.TrimSpace(*in.Comment)) >= 0 {
		user.Comment = *in.Comment
	}
	res, err := l.svcCtx.GorseClient.InsertUser(l.ctx, user)
	if err != nil {
		return nil, err
	}
	return &recommended.BaseRowAffectedResp{
		RowAffected: pointy.GetPointer(uint32(res.RowAffected)),
	}, nil
}
